import nunjucks from "nunjucks"
import {fileURLToPath} from 'url'
import fs from 'fs'

export const registryTemplate=function(){
    return async (ctx,next)=>{
        ctx.render = function (html,obj){
            let viewsBaseUrl=new URL('../views',import.meta.url);
            let viewsBasePath = fileURLToPath(viewsBaseUrl)

            let env = nunjucks.configure(viewsBasePath,{noCache:true,watch:true});
            ctx.body=env.render(html,obj)
        }
        await next()
    }
}


export const processStaticResources=function(){
    return async (ctx,next)=>{
        let url =ctx.url;

        if(url.startsWith('/statics')){
            ctx.body=fs.readFileSync(`./${url}`);
        }
        await next();
    }
}


